package com.jn.erp.web.api.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.student.entity.TRenewalAlertsConfig;
import com.jn.erp.web.api.student.entity.dto.StudentRenewalDto;
import com.jn.erp.web.api.student.mapper.RenewalAlertsMapper;
import com.jn.erp.web.api.student.service.RenewalAlertsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static com.jn.erp.security.utils.SecurityUtils.getUserId;

@Service
public class RenewalAlertsServiceImpl extends ServiceImpl<RenewalAlertsMapper, TRenewalAlertsConfig> implements RenewalAlertsConfigService {

    @Autowired
    private RenewalAlertsMapper renewalAlertsMapper;

    @Override
    public void alertSettings(StudentRenewalDto studentRenewalDto) {
        TRenewalAlertsConfig tRenewalAlertsConfig = new TRenewalAlertsConfig();
        tRenewalAlertsConfig.setRemainingClass(studentRenewalDto.getRemainingClass());
        tRenewalAlertsConfig.setRemainingDay(studentRenewalDto.getRemainingDay());
        tRenewalAlertsConfig.setIsDelete("0");
        tRenewalAlertsConfig.setCreateUser(String.valueOf(getUserId()));
        tRenewalAlertsConfig.setCreateDate(LocalDateTime.now());
        TRenewalAlertsConfig tRenewalAlertsConfig1 = renewalAlertsMapper.selectById(studentRenewalDto.getId());
        if (tRenewalAlertsConfig1.getOrgId() == studentRenewalDto.getOrgId()){
            renewalAlertsMapper.updateById(tRenewalAlertsConfig);
        }else {
            tRenewalAlertsConfig.setOrgId(studentRenewalDto.getOrgId());
            renewalAlertsMapper.insert(tRenewalAlertsConfig);
        }
    }

    @Override
    public TRenewalAlertsConfig settingsList(TRenewalAlertsConfig tRenewalAlertsConfig) {
        QueryWrapper<TRenewalAlertsConfig> qw = new QueryWrapper<>();
        qw.eq("org_id" , tRenewalAlertsConfig.getOrgId());
        qw.eq("is_delete" , 0);
        TRenewalAlertsConfig Config = renewalAlertsMapper.selectOne(qw);
        return Config;
    }
}
